package info.webest.media;

import info.webest.media.activities.MainActivity;
import info.webest.media.files.FileFilter;
import info.webest.media.files.FileManager;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FileItemAdapter extends ArrayAdapter<FileItem> {

	// private Context context;
	private ArrayList<FileItem> adapter_items;

	public FileItemAdapter(Context context, int resource,
			ArrayList<FileItem> items) {

		super(context, resource, items);
		// this.context = context;
		adapter_items = items;
	}

	@Override
	public int getCount() {
		return adapter_items.size();
	}

	@Override
	public FileItem getItem(int position) {
		return adapter_items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final FileItemHolder holder;
		final FileItem item = getItem(position);

		if (convertView == null) {

			LayoutInflater inflater = (LayoutInflater) parent.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			ViewGroup rootView = null;
			convertView = inflater.inflate(R.layout.file_item, rootView);

			holder = new FileItemHolder();
			holder.checkBox = (CheckBox) convertView
					.findViewById(R.id.chkSelected);
			holder.checkBox.setClickable(false);

			holder.icon = (ImageView) convertView.findViewById(R.id.ivIcon);
			holder.title = (TextView) convertView.findViewById(R.id.tvTitle);
			holder.size = (TextView) convertView.findViewById(R.id.tvFileSize);
			holder.duration = (TextView) convertView
					.findViewById(R.id.tvDuration);

			convertView.setTag(holder);
		} else {
			holder = (FileItemHolder) convertView.getTag();

		}

		// ListView lv = (ListView) parent;
		if (Options.CHOICE_MODE_MULTIPLE) {
			holder.getCheckBox().setVisibility(View.VISIBLE);
			holder.getCheckBox().setChecked(item.isChecked());
		} else {
			holder.getCheckBox().setVisibility(View.GONE);
		}

		// Font Size
		holder.title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Options.FontSize);
		holder.size.setTextSize(TypedValue.COMPLEX_UNIT_SP, Options.FontSize);

		// LineSpacing
		convertView.setPadding(Options.LineSpacing, Options.LineSpacing,
				Options.LineSpacing, Options.LineSpacing);

		Resources r = convertView.getResources();

		final File f = (File) (item.get("file"));

		if (f.isDirectory()) {
			holder.title.setText(item.getFileName());

			if (Options.ShowFolderSize) {
				holder.size.setText(FileManager.getSize(f));
			} else {
				holder.size.setVisibility(View.GONE);
			}

			if (Options.CountFolderElements) {
				holder.duration.setVisibility(View.VISIBLE);
				holder.duration.setText("" + item.childCount);
			} else {
				holder.duration.setVisibility(View.GONE);
			}
		} else {
			if (Options.ShowFileExt) {
				holder.title.setText(item.getFileName());
			} else {
				holder.title.setText(item.getTitle());
			}

			if (Options.ShowFileSize) {
				holder.size.setVisibility(View.VISIBLE);
				holder.size.setText(FileManager.getSize(f));
			} else {
				holder.size.setVisibility(View.GONE);
			}

			if (Options.ShowDuration) {
				holder.duration.setVisibility(View.VISIBLE);
				holder.duration.setText(item.getDuration());
			} else {
				holder.duration.setVisibility(View.GONE);
			}
		}

		// ListView lv = (ListView) parent;

		if (MainActivity.playList.isSelDir()
				&& (position == ((ListView) parent).getCheckedItemPosition())) {
			convertView.setBackgroundColor(r.getColor(R.color.selected_row));
		} else {
			convertView.setBackgroundColor(Color.TRANSPARENT);
		}

		// file name
		String name = f.getName();

		// holder.title.setTextColor(Color.WHITE);
		holder.title.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);

		// set icon
		Drawable ic = null;
		if (f.isDirectory()) {
			if (name.equals("storage") || name.contains("sdcard")) {
				ic = r.getDrawable(R.drawable.ic_folder_sdcard);
			} else if (name.equalsIgnoreCase("music")) {
				ic = r.getDrawable(R.drawable.ic_folder_music);
			} else {
				ic = r.getDrawable(R.drawable.ic_folder_dark);
			}
			// bold style
			// vHolder.title.setTextColor(Color.GRAY);
			// holder.title.setTextColor(Color.YELLOW);
			holder.title.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
		} else if (FileFilter.isImageExt(name)) {
			ic = r.getDrawable(R.drawable.ic_file_image);
		} else if (FileFilter.isAudioExt(name)) {
			ic = r.getDrawable(R.drawable.ic_file_audio);
		} else if (FileFilter.isVideoExt(name)) {
			ic = r.getDrawable(R.drawable.ic_file_video);
		} else if (FileFilter.isBookExt(name)) {
			ic = r.getDrawable(R.drawable.ic_file_book);
		}
		holder.icon.setImageDrawable(ic);
		holder.icon.setColorFilter(Color.GRAY);

		// set text color
		if (f.isHidden()) {
			// vHolder.title.setTextColor(Color.GRAY);
		}

		if (!f.canRead()) {
			// vHolder.title.setTextColor(Color.YELLOW);
		}

		return convertView;
	}

	public void setList(ArrayList<FileItem> items) {
		adapter_items.clear();
		if (items != null)
			for (FileItem item : items)
				adapter_items.add(item);
		notifyDataSetChanged();
	}
}
